package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"tik-tok-repo/handler"
)

func Cors() gin.HandlerFunc {

	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
func Init() *gin.Engine {

	r := gin.Default()
	r.Use(Cors())
	r.Static("/static", "./static")
	r.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "hello World!")
	})
	r.POST("/userLogin", handler.UserLogin)         //用户登录
	r.POST("/userRole", handler.UserRole)           //用户与角色
	r.POST("/roleNode", handler.RoleNode)           //角色与权限
	r.GET("/UserList", handler.UserList)            //用户与角色关联
	r.GET("/RoleList", handler.RoleList)            //角色与权限关联
	r.POST("/houseAdd", handler.HouseAdd)           //仓库信息添加
	r.POST("/houseOrderAdd", handler.HouseOrderAdd) //订单添加
	r.GET("/houseList", handler.HouseList)          //订单列表
	r.GET("/houseDetail", handler.HouseDetail)      //订单详情
	return r
}
